package com.jiaoxiaobo.exchange.header;

import com.jiaoxiaobo.RabbitMQUtil;
import com.rabbitmq.client.*;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

public class HeaderExchangeProducer {

    private static final String EXCHANGE_NAME = "header - logs";

    public static void main(String[] args) throws Exception {

        String routingKey = "ourTestRoutingKey";
        // The map for the headers.
        Map<String, Object> headers = new HashMap<>();
        headers.put("loglevel", "info");
        headers.put("buslevel", "product");
        headers.put("syslevel", "admin");

        String message = "LOG INFO asdfasdf";

        Connection connection = RabbitMQUtil.getConnection();
        Channel channel = connection.createChannel();

        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.HEADERS);

        AMQP.BasicProperties.Builder builder = new AMQP.BasicProperties.Builder();
        builder.deliveryMode(MessageProperties.PERSISTENT_TEXT_PLAIN.getDeliveryMode());
        builder.priority(MessageProperties.PERSISTENT_TEXT_PLAIN.getPriority());
        builder.headers(headers);

        channel.basicPublish(EXCHANGE_NAME,routingKey,builder.build(),message.getBytes(StandardCharsets.UTF_8));
        channel.close();
        connection.close();


    }

}
